/**
 * 地址的实现
 *
 * @project sky-take-out2
 * @author MECHREVO
 * @date 2023/12/10 21:14:50
 * @version 1.0
 */
package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressMapper;
import com.sky.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public void addAddress(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressMapper.addAddress(addressBook);
    }

    @Override
    public List<AddressBook> list() {
        return addressMapper.list();
    }

    @Override
    public void delete(Long id) {
        addressMapper.deleteById(id);
    }

    @Override
    public AddressBook getById(Long id) {
        return addressMapper.selectById(id);
    }

    @Override
    public void update(AddressBook addressBook) {
        addressMapper.update(addressBook);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressMapper.updateByUserId(addressBook);
        addressBook.setIsDefault(StatusConstant.ENABLE);
        addressMapper.update(addressBook);
    }

    @Override
    public AddressBook getDefault() {
        return addressMapper.getDefault();
    }
}
